<template>
  <div>
    <!--卡片-->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-button v-hasPermi="['admin','sms:seckillsession:add']" type="primary" small class="addBtn" style="margin:10px" size="mini" icon="el-icon-plus" @click="addDialogVisible = true">新增
      </el-button>
      <!--表格-->
      <el-table :data="listForm" stripe border style="width: 100%">
        <el-table-column prop="id" header-align="center" align="center" label="id" />
        <el-table-column prop="name" header-align="center" align="center" label="场次名称" />
        <el-table-column prop="startTime" header-align="center" align="center" label="每日开始时间" />
        <el-table-column prop="endTime" header-align="center" align="center" label="每日结束时间" />
        <el-table-column prop="status" header-align="center" align="center" label="启用状态" />
        <el-table-column prop="createTime" header-align="center" align="center" label="创建时间" />
        <!--操作-->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button v-hasPermi="['admin','sms:seckillsession:edit']" type="text" size="mini" @click="editBtn(scope.row.id)">编辑</el-button>
            <el-button v-hasPermi="['admin','sms:seckillsession:del']" type="text" size="mini" @click="delBtn(scope.row.id)">删除</el-button>
            <el-button v-hasPermi="['admin','sms:seckillsession:edit']" type="text" size="mini" @click="relevanceBtn(scope.row.id)">关联</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页区域-->
      <el-pagination
        background
        :current-page="pageNumber"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />

    </el-card>

    <!--添加对话框-->
    <el-dialog title="添加" :close-on-click-modal="false" :visible.sync="addDialogVisible" width="580px" @close="addHandleClose">
      <span>
        <el-form ref="addFormRef" :model="addForm" :rules="addFormRules" label-width="100px">
          <el-form-item label="场次名称" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="每日开始时间" prop="startTime">
            <el-date-picker
              v-model="addForm.startTime"
              type="datetime"
              placeholder="选择日期时间"
            />
          </el-form-item>
          <el-form-item label="每日结束时间" prop="endTime">
            <el-date-picker
              v-model="addForm.endTime"
              type="datetime"
              placeholder="选择日期时间"
            />
          </el-form-item>
          <el-form-item label="启用状态">
            <el-switch
              v-model="addForm.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <!--底部区域-->
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addClick">确 定</el-button>
      </span>
    </el-dialog>

    <!--编辑对话框-->
    <el-dialog title="编辑" :close-on-click-modal="false" :visible.sync="editDialogVisible" width="580px" @close="editHandleClose">
      <span>
        <el-form ref="editFormRef" :model="editForm" label-width="100px">
          <el-form-item label="场次名称" prop="name">
            <el-input v-model="editForm.name" />
          </el-form-item>
          <el-form-item label="每日开始时间" prop="startTime">
            <el-date-picker
              v-model="editForm.startTime"
              type="datetime"
              placeholder="选择日期时间"
            />
          </el-form-item>
          <el-form-item label="每日结束时间" prop="endTime">
            <el-date-picker
              v-model="editForm.endTime"
              type="datetime"
              placeholder="选择日期时间"
            />
          </el-form-item>
          <el-form-item label="启用状态">
            <el-switch
              v-model="editForm.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <!--底部区域-->
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editClick">确 定</el-button>
      </span>
    </el-dialog>

    <!--关联对话框-->
    <el-dialog title="关联" :close-on-click-modal="false" :visible.sync="relevanceDialogVisible" width="580px">
      <span>
        <el-button slot="reference" type="primary" small style="margin:10px" size="mini" icon="el-icon-plus" @click="addRelevanceBtn()">新增
        </el-button>
        <!--表格-->
        <el-table :data="listRelevanceForm" stripe border style="width: 100%">
          <el-table-column prop="id" header-align="center" align="center" label="id" />
          <el-table-column prop="promotionSessionId" header-align="center" align="center" label="活动场次id" />
          <el-table-column prop="skuId" header-align="center" align="center" label="商品id" />
          <el-table-column prop="seckillPrice" header-align="center" align="center" label="秒杀价格" />
          <el-table-column prop="seckillCount" header-align="center" align="center" label="秒杀总量" />
          <el-table-column prop="seckillLimit" header-align="center" align="center" label="每人限购数量" />
          <el-table-column prop="seckillSort" header-align="center" align="center" label="排序" />
          <!--操作-->
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-tooltip :enterable="false" effect="dark" content="删除" placement="top">
                <el-button type="danger" icon="el-icon-delete" size="mini" @click="delcategorybrandrelationBtn(scope.row.id)" />
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <!--底部区域-->
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="relevanceDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <!--新增关联对话框-->
    <el-dialog title="关联" :close-on-click-modal="false" :visible.sync="addRelevanceDialogVisible" width="580px">
      <span>
        <el-form ref="addRelationFormRef" :model="addRelationForm" label-width="100px">
          <el-form-item label="活动场次id" prop="promotionSessionId">
            <el-input v-model="addRelationForm.promotionSessionId" :disabled="true" />
          </el-form-item>
          <el-form-item label="商品id" prop="skuId">
            <el-input v-model="addRelationForm.skuId" />
          </el-form-item>
          <el-form-item label="秒杀价格" prop="seckillPrice">
            <el-input-number v-model="addRelationForm.seckillPrice" :precision="2" :step="0.1" :max="10" />
          </el-form-item>
          <el-form-item label="秒杀总量">
            <el-input-number v-model="addRelationForm.seckillCount" :min="1" :max="10" label="描述文字" />
          </el-form-item>
          <el-form-item label="每人限购">
            <el-input-number v-model="addRelationForm.seckillLimit" :min="1" :max="10" label="描述文字" />
          </el-form-item>
          <el-form-item label="排序">
            <el-input v-model="addRelationForm.seckillSort" />
          </el-form-item>
        </el-form>
        <!--底部区域-->
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addRelevanceDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addRelevanceClick">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { add, edit, del, getSeckillSession, listSeckillSession } from '@/api/coupon/seckillsession'
import { addRelation, listSeckillSkuRelation } from '@/api/coupon/seckillskurelation'

export default {
  name: 'Session',
  data() {
    return {
      addDialogVisible: false,
      editDialogVisible: false,
      relevanceDialogVisible: false,
      addRelevanceDialogVisible: false,
      pageNumber: 1,
      pageSize: 10,
      total: 0,
      listForm: [],
      listRelevanceForm: [],
      addForm: {
        name: '',
        startTime: '',
        endTime: '',
        status: 1
      },
      addRelationForm: {
        promotionSessionId: '',
        skuId: '',
        seckillPrice: '',
        seckillCount: 1,
        seckillLimit: 1,
        seckillSort: 0
      },
      editForm: {},
      addFormRules: {
        name: [
          { required: true, message: '请输入场次名称', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '请输入每日开始时间', trigger: 'blur' }
        ],
        endTime: [
          { required: true, message: '请输入每日结束时间', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 初始化列表
    this.initListSeckillSession()
  },

  methods: {
    // 添加按钮
    addBtn() {
      this.addDialogVisible = true
    },

    // 确认添加
    addClick() {
      this.$refs.addFormRef.validate(valid => {
        if (!valid) return
        add(this.addForm).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.initListSeckillSession()
          }
          // 关闭对话框
          this.addDialogVisible = false
        })
      })
    },

    // 编辑按钮
    editBtn(id) {
      this.editDialogVisible = true
      getSeckillSession(id).then(res => {
        if (res.code === 20000) {
          this.editForm = res.data
        }
      })
    },

    // 确认编辑
    editClick() {
      this.$refs.editFormRef.validate(valid => {
        if (!valid) return
        edit(this.editForm).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.initListSeckillSession()
          }
          // 关闭对话框
          this.editDialogVisible = false
        })
      })
    },

    // 删除按钮
    delBtn(id) {
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确认操作
        del(id).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.initListSeckillSession()
          }
        })
      })
    },

    // 关联按钮
    relevanceBtn(id) {
      this.relevanceDialogVisible = true
      // 查询关联的商品信息
      const params = {
        promotionSessionId: id,
        pageNum: this.pageNumber,
        pageSize: this.pageSize
      }
      this.addRelationForm.promotionSessionId = id
      listSeckillSkuRelation(params).then(res => {
        this.listRelevanceForm = res.data.records
        this.total = res.data.total
        this.pageNum = res.data.current
      })
    },

    // 新增关联关联
    addRelevanceBtn() {
      this.addRelevanceDialogVisible = true
      // 查询关联的商品信息
      this.initListSeckillRelation(this.addRelationForm.promotionSessionId)
    },
    // 确认关联
    addRelevanceClick() {
      this.addRelevanceDialogVisible = false
      addRelation(this.addRelationForm).then(res => {
        if (res.code === 20000) {
          this.$message.success(res.message)
          // 刷新数据
          this.initListSeckillRelation(this.addRelationForm.promotionSessionId)
        }
        // 关闭对话框
        this.addRelevanceDialogVisible = false
      })
    },

    // 监听当前页的事件
    handleCurrentChange(pageIndex) {
      this.pageNumber = pageIndex
      this.initListSeckillSession()
    },

    // 监听当前页的条数事件
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.initListSeckillSession()
    },

    // 监听添加对话框的关闭事件，重置表单内容
    addHandleClose() {
      this.$refs.addFormRef.resetFields()
    },

    // 监听编辑对话框的关闭事件,重置表单内容
    editHandleClose() {
      this.$refs.editFormRef.resetFields()
    },

    // 初始化数据
    initListSeckillSession() {
      const params = {
        pageNum: this.pageNumber,
        pageSize: this.pageSize
      }
      listSeckillSession(params).then(res => {
        this.listForm = res.data.records
        this.total = res.data.total
        this.pageNum = res.data.current
      })
    },

    // 初始化数据
    initListSeckillRelation(promotionSessionId) {
      const params = {
        promotionSessionId: promotionSessionId,
        pageNum: this.pageNumber,
        pageSize: this.pageSize
      }
      listSeckillSkuRelation(params).then(res => {
        this.listRelevanceForm = res.data.records
        this.total = res.data.total
        this.pageNum = res.data.current
      })
    }
  }
}
</script>

<style scoped>
</style>
